--#include "data\config\activityconfig\BackStageActivity\TotalCostConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once         --排行榜管理
--#include "data\functions\Activity\BackStageActivity\BackStageActivity.lua"   once         -- 后台活动开启关闭管理

--累计消费开始
function StartTotalCost()
    local ssVar = System.getStaticVar()
    ssVar.totalCost = 1
    ssVar.totalCostEnd = nil
    gRankMgr_Clear(RANK_DEFINE_TOTAL_COST)
    OpenBackStageActivity(CommonActivity_TotalCost)
end

--累计消费结束
function EndTotalCost()
    local ssVar = System.getStaticVar()
    --ssVar.totalCost = nil
    ssVar.totalCostEnd = 1
    CloseBackStageActivity(CommonActivity_TotalCost)
end


--跨天发送邮件
function TotalCostSendMailTotal()
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
        return
    end
    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_TOTAL_COST].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local actorid = tonumber(Ranking.getId(item))
            local subIndex = ssVar.totalCost-1
            local data = Ranking.getSub(item, subIndex)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end
            TotalCostSendMail(actorid, dataTable)
        end
    end
end


--服务器跨天
function TotalCostCrossDay()
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
        return
    end

    if not ssVar.totalCostEnd then
        return
    end

    TotalCostSendMailTotal()

    if not System.IsCommonActivityOpen(CommonActivity_TotalCost) then
        ssVar.totalCost = nil
    end
end

--累计消费元宝
function TotalCostConsumeYuanBao(sysarg, Gold)
    if not System.IsCommonActivityOpen(CommonActivity_TotalCost) then
        return
    end
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
        return
    end
    if ssVar.totalCostEnd and ssVar.totalCostEnd == 1 then
        return
    end
    local nPlanId = System.GetCommonActivityParam(CommonActivity_TotalCost,0,0,0,0)
    local cfg = TotalCostCfg[nPlanId] 
    if not cfg then
        cfg = TotalCostCfg[#TotalCostCfg]
    end

    local itemIndex = ssVar.totalCost 
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TOTAL_COST, itemIndex)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end
    if not myData.myGold then
        myData.myGold = 0
    end
    myData.myGold = myData.myGold + Gold
    if not myData.myGoldData then
        myData.myGoldData = {}
    end
    for idx = 1, #cfg.Gold do
        if myData.myGoldData[idx] and myData.myGoldData[idx] >= 1 then
        else
            if cfg.Gold[idx] <= myData.myGold then
                myData.myGoldData[idx] = 1
            end
        end
    end
    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_TOTAL_COST, itemIndex, newData)
    SendTotalCostData(sysarg, nil, myData)
end

--跨天邮件玩家
function TotalCostSendMail(actorId, dataTable)
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
        return
    end
    local nPlanId = System.GetCommonActivityParam(CommonActivity_TotalCost,0,0,0,0)
    local cfg = TotalCostCfg[nPlanId] 
    if not cfg then
        cfg = TotalCostCfg[#TotalCostCfg]
    end
    if not dataTable then
        return
    end

    if not dataTable.myGoldData then
        return
    end

    local rewardCfg = cfg.Rewards

    for idx = 1, #cfg.Gold do
        if dataTable.myGoldData[idx] and dataTable.myGoldData[idx] == 1 then
            local rewards = rewardCfg[idx]
            local title = Lang.ScriptTips.TotalCost001
            local content = Lang.ScriptTips.TotalCost002
            Awards.GiveToMailById(actorId, rewards, title, content, LogIds.TotalCostLogId, Lang.LogTips.TotalCost)
        end
    end
end

--发送累计消费数据
function SendTotalCostData(sysarg, packet, myData)
    if not System.IsCommonActivityOpen(CommonActivity_TotalCost) then
        return
    end
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
        return
    end
    if ssVar.totalCostEnd and ssVar.totalCostEnd == 1 then
        return
    end
    local nPlanId = System.GetCommonActivityParam(CommonActivity_TotalCost,0,0,0,0)
    if nPlanId <= 0 or nPlanId > #TotalCostCfg then
        nPlanId = #TotalCostCfg
    end
    local cfg = TotalCostCfg[nPlanId] 

    if not myData then
        local itemIndex = ssVar.totalCost
        local actorid = Actor.getActorId(sysarg)
        local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TOTAL_COST, itemIndex)

        myData = BaseFuc_unserialize(data)
    end

    if not myData then
        myData = {}
    end

    if not myData.myGold then
        myData.myGold = 0
    end
    if not myData.myGoldData then
        myData.myGoldData = {}
    end


    local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetTotalCostInfo)
    if (pack == nil) then
        return
    end
    DataPack.writeByte(pack, nPlanId)
    local LeftTime = System.GetCommonActivityLeftTime(CommonActivity_TotalCost)
    DataPack.writeUInt(pack, LeftTime or 0)
    DataPack.writeUInt(pack, myData.myGold or 0)
    local len = #cfg.Gold
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local state = myData.myGoldData[idx] or 0
        DataPack.writeByte(pack, state)
    end
    DataPack.flush(pack)
end

--领取奖励
function GetTotalCostAwards(sysarg, packet)
    local Index = DataPack.readByte(packet)
    if not System.IsCommonActivityOpen(CommonActivity_TotalCost) then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost003, ttFlyTip)
        return
    end
    local ssVar = System.getStaticVar()
    if not ssVar.totalCost then
         Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost003, ttFlyTip)
        return
    end
    if ssVar.totalCostEnd and ssVar.totalCostEnd == 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost004, ttFlyTip)
        return
    end
    local nPlanId = System.GetCommonActivityParam(CommonActivity_TotalCost,0,0,0,0)
    local cfg = TotalCostCfg[nPlanId] 
    if not cfg then
        cfg = TotalCostCfg[#TotalCostCfg]
    end
    if Index > #cfg.Gold then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost005, ttFlyTip)
        return
    end

    local itemIndex = ssVar.totalCost 
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TOTAL_COST, itemIndex)
    local myData = BaseFuc_unserialize(data)

    if not myData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost006, ttFlyTip)
        return
    end
     if not myData.myGoldData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost006, ttFlyTip)
        return
    end
    if not myData.myGoldData[Index] or myData.myGoldData[Index] ~= 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.TotalCost006, ttFlyTip)
        return
    end

    local rewardCfg = cfg.Rewards

    local rewards = rewardCfg[Index]

    myData.myGoldData[Index] = 2
    
    if IsBagGridEnough(sysarg, rewards) then
        GiveCommonAward(sysarg, rewards, LogIds.TotalCostLogId, Lang.LogTips.TotalCost)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.TotalCost001
        local content = Lang.ScriptTips.TotalCost002
        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.TotalCostLogId, Lang.LogTips.TotalCost)
    end

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_TOTAL_COST, itemIndex, newData)

    local len = #cfg.Gold
    local endState = true
    for idx = 1, len do
        local state = myData.myGoldData[idx] or 0
        if state ~= 2 then
            endState = false
        end
    end
    if endState then
        CloseBackStageActivity(CommonActivity_TotalCost, sysarg)
    else
        SendTotalCostData(sysarg, nil, myData)
    end
end

--累计消费是否全部领取
function GetTotalCostState(sysarg)
    local ssVar = System.getStaticVar()
    if not System.IsCommonActivityOpen(CommonActivity_TotalCost) then
        --兼容处理 在需要的时候开启
        --[[
        if ssVar.totalCost == 1 and ssVar.totalCostEnd == nil then
            ssVar.totalCostEnd = 1
            TotalCostCrossDay()
        end
        ]]
        return true
    end
    
    if not ssVar.totalCost then
        return true
    end
    if ssVar.totalCostEnd and ssVar.totalCostEnd == 1 then
        return true
    end
    local nPlanId = System.GetCommonActivityParam(CommonActivity_TotalCost,0,0,0,0)
    if nPlanId <= 0 or nPlanId > #TotalCostCfg then
        nPlanId = #TotalCostCfg
    end
    local cfg = TotalCostCfg[nPlanId] 

   
    local itemIndex = ssVar.totalCost
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TOTAL_COST, itemIndex)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myGold then
        myData.myGold = 0
    end
    if not myData.myGoldData then
        myData.myGoldData = {}
    end

    local len = #cfg.Gold
    local endState = true
    for idx = 1, len do
        local state = myData.myGoldData[idx] or 0
        if state ~= 2 then
            endState = false
        end
    end
    if endState then
        return true
    end
    return false
end